package com.controller;

import com.api.CommonResult;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.BTypeService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.entity.BGoods;
import com.service.BGoodsService;

import java.util.List;

/**
 * 商品表 控制层。
 *
 * @author admin
 * @since 2024-05-30
 */
@Controller
@RequestMapping("/admin/good")
public class BGoodsController {

    @Autowired
    private BGoodsService bGoodsService;

    @Autowired
    private BTypeService bTypeService;

    //商品列表
    @GetMapping("/goodList")
    public String goodList(ModelMap model, @RequestParam(value = "goodName", defaultValue = "") String goodName,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        System.out.println("Test 参数映射打印99999999999999999999999999999999999999999999999 ： " + goodName);
        try {
            QueryWrapper queryWrapper;
//            if (goodName.equals("")) {
//                queryWrapper = QueryWrapper.create();
//            } else {
//                queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
//            }
            queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page, queryWrapper);
            System.out.println("检查属性值是否为 null : " + pageInfo);
//            String typename = pageInfo.getRecords().get(0).getBType().getTypeName(); //无用代码
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "back/goods-list";
    }

    @GetMapping(value = "/addView")
    public String addView(Model model) {
        List<BType> typeList = bTypeService.list();
        model.addAttribute("typeList", typeList);
        return "back/goods-add";
    }

    /**
     * 添加商品表。
     *
     * @param bGoods 商品表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/save")
    @ResponseBody
    public CommonResult save(@ModelAttribute BGoods bGoods) {
        try {
            Boolean r = bGoodsService.save(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @GetMapping("/updateView")
    public String updateView(int id, Model model) {
        List<BType> typeList = bTypeService.list();
        BGoods bGood = bGoodsService.getById(id);
        model.addAttribute("typeList", typeList);
        model.addAttribute("bGood", bGood);
        return "back/goods-edit";
    }

    /**
     * 根据主键更新商品表。
     *
     * @param bGoods 商品表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@ModelAttribute BGoods bGoods) {
        try {
            Boolean r = bGoodsService.updateById(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("/del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            Boolean r = bGoodsService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

}
